<?php
namespace Home\Controller;

use Model\CommentsModel;
use Model\LiveSettingModel;
use Model\LiveUrlModel;
use Model\PraiseModel;
use Model\RoomModel;
use Model\SubjectsModel;
use Model\TeacherModel;
use Model\UserModel;
use Think\Controller;

class OnlineController extends BaseController
{

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $tid = I("get.tid");
        $subjectsModel = new SubjectsModel();
        $subjectsList = $subjectsModel->select();
        if (empty($tid)) {
            $tid = 0;
            $roomModel = new RoomModel();
            $where['beginTime'] = array("EGT", time());
            $roomList = $roomModel->where($where)->select();
        } else {
            $roomModel = new RoomModel();
            $roomList = $roomModel->table("live_rooms rooms,live_setting setting")->where(" `setting`.`teacherId` = {$tid}")->field("rooms.*")->select();
        }

        foreach ($roomList as $k => $v) {
            $roomList[$k]['teacher'] = $this->getTeacherMeaage($v['liveUrlId']);
        }

        $this->assign("tid", $tid);
        $this->assign("roomList", $roomList);
        $this->assign("subjectsList", $subjectsList);
        $this->display();
    }

    public function play()
    {
        $id = I("get.id");
        $roomModel = new RoomModel();
        $room = $roomModel->find($id);
        $liveSettingModel = new LiveSettingModel();
        $liveSetting = $liveSettingModel->where("`roomId` = {$room['id']}")->find();
        if (empty($liveSetting)) {
            $this->error("参数异常", "/");
        }
        $liveUrlsModel = new LiveUrlModel();
        $liveUrl = $liveUrlsModel->find($liveSetting['liveUrlId']);
        if (empty($liveUrl)) {
            $this->error("参数异常", "/");
        }
        $teacherModel = new TeacherModel();
        $teacher = $teacherModel->find($liveSetting['teacherId']);
        if (empty($teacherModel)) {
            $this->error("参数异常", "/");
        }
        $data = [
            'teacher' => $teacher,
            'liveUrl' => $liveUrl,
            'liveSetting' => $liveSetting,
            'room' => $room
        ];

        $commentsModel = new CommentsModel();
        $commentsList = $commentsModel->table("comments cm,users ue")->where("cm.uid = ue.id")->field("cm.*,ue.nickname,ue.picture")->order("cm.id desc")->select();
        $count = count($commentsList);
        $this->assign("count", $count);
        $this->assign("commentsList", $commentsList);
        $this->assign("data", $data);
        $this->display();
    }

    public function comments()
    {
        $commentsModel = new CommentsModel();
        $data = $commentsModel->create();
        $data['time'] = date("Y-m-d h:i:s");
        $insertComments = $commentsModel->add($data);
        if ($insertComments) {
            $userModel = new UserModel();
            $user = $userModel->find($data['uid']);
            $return['status'] = "ok";
            $return['id'] = $insertComments;
            $return['picture'] = $user['picture'];
            $return['nickname'] = $user['nickname'];
            $return['message'] = $data['message'];
            $return['time'] = $data['time'];
        } else {
            $return['status'] = "error";
            $return['sql'] = $commentsModel->getLastSql();
        }

        echo json_encode($return);

    }

    public function ding()
    {
        $commentsModel = new CommentsModel();
        $praiseModel = new PraiseModel();
        $data['cid'] = I("post.cid");
        $data['uid'] = I("post.uid");
        $data['rid'] = I("post.rid");
        if (empty($data['cid']) || empty($data['uid']) || empty($data['rid'])) {
            echo json_encode(['status' => 'error']);
            return;
        }
        $findPraise = $praiseModel->where($data)->find();
        if (!empty($findPraise)) {
            $return['status'] = "haved";
            echo json_encode(['status' => 'haved']);
            return;
        }
        $commentsModel->where("`id` = {$data['cid']}")->setInc("praise");
        if ($commentsModel) {
            $praiseModel->add($data);
            $return['status'] = "ok";
            echo json_encode(['status' => 'ok']);
            return;
        } else {
            $return['status'] = "error";
            echo json_encode(['status' => 'error']);
            return;
        }


    }

    /**
     * @param $liveUrlId
     * @return array
     */
    public function getTeacherMeaage($liveUrlId)
    {
        $teacherModel = new TeacherModel();
        $liveSetting = new LiveSettingModel();
        $settingMessage = $liveSetting->where(" `liveUrlId` = {$liveUrlId} ")->find();

        $teacher = $teacherModel->find($settingMessage['teacherId']);

        switch ($teacher['level']) {
            case 1:
                $teacher['levelTitle'] = "特级教师";
                break;
            case 2:
                $teacher['levelTitle'] = "普通教师";
                break;

        }


        return $teacher;
    }

    public function getToken(){
        $userId = cookie("id");
        $token = C("TOKEN");
        $roomId  = I("post.rid");
        $userModel = new UserModel();
        if(empty($userId)){
            //游客
            $this->ajaxReturn(['token'=>md5($roomId.$token),"ip"=>C("SOCKET_SERVER_IP"),"port"=>C("SOCKET_SERVER_PORT"),'type'=>'connect']);
        }else{
            //用户
            $userModel = new UserModel();
            $findUser = $userModel->find($userId);
            $this->ajaxReturn(['userId'=>$userId,'userName'=>$findUser['nickname'],'token'=>md5($roomId . $userId . $findUser['nickname'] . $token),"ip"=>C("SOCKET_SERVER_IP"),"port"=>C("SOCKET_SERVER_PORT"),'type'=>'login']);
        }
    }
}